<?php
namespace view;
Class Star {

    private $points;

    /**
     *
     * @var \ImagickDraw
     */
    private $draw;
    /**
     *
     * @var \Imagick
     */
    private $image;
    private $starSize;
    public $transparent=false;
    private function createPointList($points) {
        $rows = explode(" ", $points);
        $result = array();
        foreach ($rows as $row) {
            $cols = explode(",", "$row");
            $point["x"] = floatval($cols[0]) *($this->starSize/40);
            $point["y"] = floatval($cols[1]) *($this->starSize/40);
            $result[] = $point;
        }
        return $result;
    }
    /**
     * 
     * @param int $starSize <p>rozmiar pojedynczej gwiazdy</p>
     * @param float $number <p>ocena, która zostanie przedstawiona za pomocą gwiazdek</p>
     * @param int $grey [optional[ <p>ilość gwiazdek</p>
     */
    public function __construct($starSize,$number,$grey=0,$transparent=false) {
        $this->transparent=$transparent;
        $this->starSize = $starSize;
        $this->draw = new \ImagickDraw();
        $this->draw->translate(-$this->starSize, 0);
        $this->points[0][0]=["x"=>0,"y"=>0];
        $this->points[10] = $this->createPointList("4,15.068 0.802,15.537 4,18.645");
        $this->points[20] = $this->createPointList("8,14.48 0.802,15.537 8,22.532");
        $this->points[30] = $this->createPointList("12,13.894 0.802,15.537 10.447,24.91 8.192,38.17 12,36.161");
        $this->points[40] = $this->createPointList("15.933,9.875 14.109,13.584 0.802,15.537 10.447,24.91 8.192,38.17 16,34.051");
        $this->points[50] = $this->createPointList("20.064,1.556 14.109,13.584 0.802,15.813 10.447,24.91 8.192,38.17 20,31.94");
        $this->points[60] = $this->createPointList("23.656,8.808 20,1.515 14.109,13.584 0.802,15.813 10.447,24.91 8,38.17 20,31.94 23.656,33.764");
        $this->points[70] = $this->createPointList("28,13.853 26.014,13.567 20.064,1.643 14.109,13.584 0.802,15.813 10.447,24.91 8,38.17 20,31.94 28,36.04");
        $this->points[80] = $this->createPointList("32,14.428 26.014,13.567 19.938,1.643 14.109,13.584 0.802,15.813 10.434,24.625 8,38.17 20,31.94 32,38.17 29.709,24.883 32.113,22.532");
        $this->points[90] = $this->createPointList("36,15.068 26.014,13.567 20,1.515 14.109,13.584 0.802,15.813 10.434,24.625 8,38.17 20,31.94 32.113,38.17 29.709,24.883 36,18.733");
        $this->points[100] = $this->createPointList("32.001,38.136 20.088,31.895 8.192,38.17 10.447,24.91 0.802,15.537 14.109,13.584 20.044,1.515 26.014,13.567 39.326,15.482 29.709,24.883");
        $round = round($number, 1);
        $percent = round($round * 100);
        $this->drawSingleStar($percent);
        $this->image = new \Imagick();       
        $oImage = new \Imagick();
        if ($grey==0){
            if (!$this->transparent){
                $this->image->newImage($starSize, $starSize, new \ImagickPixel("white"));
                $oImage->newPseudoImage($starSize,$starSize,'radial-gradient:#000-#aaa');
            }
            else {
                $this->image->newImage($starSize, $starSize, new  \ImagickPixel('transparent'));
                $oImage->newImage($starSize, $starSize, new  \ImagickPixel('transparent'));
            }
            
        }
        else if ($grey==1){
            if (!$this->transparent){
            $this->image->newImage($starSize, $starSize, new \ImagickPixel("white"));
            $oImage->newPseudoImage($starSize,$starSize,'radial-gradient:#444-#aaa');
            }
             else {
                $this->image->newImage($starSize, $starSize, new  \ImagickPixel('transparent'));
                $oImage->newImage($starSize, $starSize, new  \ImagickPixel('transparent'));
            }
        }
        else {
            if (!$this->transparent){
            $this->image->newImage($starSize, $starSize, new \ImagickPixel("grey"));
            $oImage->newPseudoImage($starSize,$starSize,'radial-gradient:#666-#aaa');
            }
             else {
                $this->image->newImage($starSize, $starSize, new  \ImagickPixel('transparent'));
                $oImage->newImage($starSize, $starSize, new  \ImagickPixel('transparent'));
            }
        }
        $this->image->setImageFormat("png");
        $this->image->drawImage($this->draw);
        $this->image->compositeImage( $oImage, \Imagick::COMPOSITE_BLEND, 0, 0 );
    }

    private function drawSingleStar($percent) {
        $this->draw->translate($this->starSize, 0);
        if (!$this->transparent){
        $this->draw->setFillColor(new \ImagickPixel("#333"));
        }
        else{
          $this->draw->setFillColor(new \ImagickPixel("#777")); 
        }
        $this->draw->polyline($this->points[100]);
        $this->draw->setFillColor(new \ImagickPixel("yellow"));
        $this->draw->polyline($this->points[$percent]);
        //newPseudoImage(150,150,'radial-gradient:#e0ad0e-#f5ee77');
    }
    public function __toString() {
        return "<img src='data:image/jpg;base64," . base64_encode($this->image->getImageBlob()) . "' />";
    }
}